package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.OrdersDto;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    //提交订单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders order){

        orderService.submit(order);
        return R.success("新增成功");
    }

    //分页查询订单
    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){

        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();


        Page<Orders> ordersPage = new Page<>(page,pageSize);

        Page<OrdersDto> ordersDtoPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(userId != null,Orders::getUserId,userId);
        queryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(ordersPage, queryWrapper);

        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");

        List<Orders> records = ordersPage.getRecords();

        List<OrdersDto> ordersDtoRecords = records.stream().map((item)->{
            OrdersDto ordersDto = new OrdersDto();

            BeanUtils.copyProperties(item,ordersDto);

            Long orderId = item.getId();

            LambdaQueryWrapper<OrderDetail> qw = new LambdaQueryWrapper<>();

            qw.eq(orderId != null,OrderDetail::getOrderId,orderId);

            List<OrderDetail> orderDetails = orderDetailService.list(qw);

            ordersDto.setOrderDetails(orderDetails);

            return ordersDto;

        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtoRecords);

        return R.success(ordersDtoPage);
    }

    @PostMapping("/again")
    public R<String> orderAgain(@RequestBody Map<String,String> map){

        //核心是通过orderId找到订单中所有的套餐或菜品，然后将这些套餐或菜品加入购物车

        //获取orderId
        String orderId = map.get("id");

        //获取用户id
        Long userId = BaseContext.getCurrentId();

        //查询orderDetail表

        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(orderId != null, OrderDetail::getOrderId,orderId);

        List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper);

        //返回一个购物车订单表
        List<ShoppingCart> shoppingCarts = orderDetails.stream().map((item)->{
            ShoppingCart shoppingCart = new ShoppingCart();

            //把对应的属性直接复制过去
            BeanUtils.copyProperties(item,shoppingCart);

            shoppingCart.setUserId(userId);

            shoppingCart.setCreateTime(LocalDateTime.now());

            return shoppingCart;

        }).collect(Collectors.toList());

        shoppingCartService.saveBatch(shoppingCarts);

        return R.success("再来一单");
    }

    //服务端订单分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,Long number,String beginTime,String endTime){

        Page<Orders> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(number != null,Orders::getNumber,number);

        //订单提交时间大于开始时间
        queryWrapper.gt(beginTime != null,Orders::getCheckoutTime,beginTime);
        //订单提交时间小于结束时间
        queryWrapper.lt(endTime != null,Orders::getCheckoutTime,endTime);

        queryWrapper.orderByAsc(Orders::getCheckoutTime);

        Page<Orders> ordersPage = orderService.page(pageInfo, queryWrapper);

        return R.success(ordersPage);
    }

    //取消，派送，完成
    @PutMapping
    public R<String> update(@RequestBody Map<String,String> map){

        String id = map.get("id");

        String status = map.get("status");
        
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.set(Orders::getStatus,status).eq(Orders::getId,id);

        orderService.update(updateWrapper);

        return R.success("成功");
    }
}
